package components
{
	import UIFree.ObjectEvent;
	import UIFree.components.uif_window;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import components.CInterface.IWindow;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class c_window extends uif_window implements IWindow {
		public static const TITLEHEIGH: int = 25;
		
		public static const DOCK_LEFT	: int = 1;
		public static const DOCK_TOP	: int = 2;
		public static const DOCK_RIGHT	: int = 4;
		public static const DOCK_BOTTOM	: int = 8;
		
		private var _resizeParentEvent	: int;
		private var _docketTypes		: int;
		
		public function c_window(parent:uif_component, awidth:int=0, aheight:int=0, a_title: String='', 
								 	modal:Boolean=false, canClose:int=2, a_styleClass: Object=null) {
			super(parent, 0, 0, awidth, aheight, String(a_styleClass?a_styleClass:'window'), modal, canClose);
			startPosition();
			title.text = a_title;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(uif_component.ENDDRAG, doEndDrag);
		}
		
		private function doEndDrag(e: Event): void {
			if (_docketTypes > 0) magnet(0); 
		}
		
		protected function startPosition(): void {
			toCenter();
		}
		
		protected function magnet(docketTypes: int): void {
			if (_resizeParentEvent > 0) removeEvent(_resizeParentEvent);
			if (docketTypes > 0) _resizeParentEvent = addEvent(uif_component.RESIZE, doParentResize, parent);
			_docketTypes = docketTypes;
			updateDockPosition();
		}
		
		protected function doParentResize(e: Event): void {
			updateDockPosition();
		}
		
		protected function updateDockPosition(): void {
			if (_docketTypes > 0) {
				var inner: Rectangle = parentComponent.getInner();
				if (_docketTypes & DOCK_LEFT) x = inner.x;
				else if (_docketTypes & DOCK_RIGHT) x = inner.x + inner.width - width;
				if (_docketTypes & DOCK_TOP) y = inner.y;
				else if (_docketTypes & DOCK_BOTTOM) y = inner.y + inner.height - height;
			}
		}
		
		protected function get locale(): Object {
			return cl.classes.locale;
		}
		
		protected function get cl(): collages {
			return app as collages;
		}
		
		override protected function get titleRect(): Rectangle {
			var inner: Rectangle 	= getInner();
			inner.height 			= TITLEHEIGH;
			return inner;
		}
		
		override protected function createCloseButton(): uif_component {
			return new c_closeButton(this);
		}
		
		override protected function createTitle():uif_label {
			return super.createTitle();
		}
		
		public function get clientBounds(): Rectangle {
			return getInner(0, TITLEHEIGH, 0, 0);
		}
	}
}